package com.test38_集合;

import org.junit.Test;

import java.util.Deque;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Queue队列，先进先出(FIFO)
 * JDK1.5提供
 */
public class Demo07_Queue {

    @Test
    public void test1() {
        Queue<String> queue = new LinkedList<>();
        System.out.println("【向队列增加数据】" + queue.offer("www.yootk.com"));
        System.out.println("【向队列增加数据】" + queue.offer("edu.yootk.com"));
        System.out.println("【向队列增加数据】" + queue.offer("yootk.ke.qq.com"));
        while (!queue.isEmpty()) {
            System.out.println("【通过队列获取数据】" + queue.poll());
        }
    }

    /**
     * Dequeue双端队列
     * JDK1.6提供
     */
    @Test
    public void test2() {
        Deque<String> deque = new LinkedList<>();
        deque.offerFirst("【FIRST】www.yootk.com");
        deque.offerLast("【LAST】edu.yootk.com");
        System.out.println(deque.pollFirst());
        System.out.println(deque.pollLast());
    }

    /**
     * PriorityQueue，优先级队列
     */
    @Test
    public void test3() {
        Queue<Integer> queue = new PriorityQueue<>();
        int[] array = {15, 36, 29, 58, 66, 20};
        for (int num : array) {
            queue.offer(num);
        }
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }

    /**
     * PriorityQueue，使用自定义比较器
     */
    @Test
    public void test4() {
        Queue<Student> queue = new PriorityQueue<>((o1, o2) -> {
                return o1.getNo() - o2.getNo();
        });
        queue.offer(new Student(25, "张三", 20));
        queue.offer(new Student(30, "李四", 22));
        queue.offer(new Student(18, "王五", 24));
        while (!queue.isEmpty()) {
            System.out.println(queue.poll());
        }
    }
}

class Student {
    private int no;
    private String name;
    private int age;

    public Student(int no, String name, int age) {
        this.no = no;
        this.name = name;
        this.age = age;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "【Student】编号：" + this.no + "、姓名：" + this.name + "、年龄：" + this.age;
    }
}
